package com.doit.day01;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;

public class ConsumerDemo {
    public static void main(String[] args) throws InterruptedException {

        Properties props = new Properties();
        /**
         * 必须的配置
         */
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        //消费者组
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g001212");
        //选配的
        //运行自动创建topic
        props.setProperty(ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG,"true");
        //指定从什么位置开始读取数据   有时候没有区别  失效
        //从上一次消费的偏移量开始消费
        //程序启动以后，首先会看程序有没有指定从哪个分区的那个偏移量开始消费 ==》如果指定了，以程序中设置的为准
        // 会首先去__consumer_offset 这个topic中找 g002 之前消费这个topic消费到了哪里
        //如果找到了，就紧接着上次消费的地方开始消费
        //如果没有找到，才会看下面这个AUTO_OFFSET_RESET_CONFIG参数
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        //允许自动提交偏移量 __consumer_offset
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        //每隔多长时间提交一次偏移量
        props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"3000");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

        //订阅主题
//        consumer.subscribe(Arrays.asList("wangbaohua002"));//==>指定所有分区的数据进行消费

        /**
         * 有没有可能出现漏读数据
         * 重复读数据的可能性
         * 如果有可能，什么情况下会重复读，什么情况下会漏读
         */

       TopicPartition tp1 =  new TopicPartition("wangbaohua002",0);
       TopicPartition tp2 =  new TopicPartition("wangbaohua002",1);
//       TopicPartition tp3 =  new TopicPartition("wangbaohua002",3);
//       TopicPartition tp4 =  new TopicPartition("wangbaohua002",2);
        consumer.assign(Arrays.asList(tp1,tp2));

        //指定消费哪个topic中的那个分区的数据，并且还能够指定从哪个offset开始
        consumer.seek(new TopicPartition("wangbaohua002",0),0);
        consumer.seek(new TopicPartition("wangbaohua002",1),0);
//        consumer.seek(new TopicPartition("wangbaohua002",2),0);
//        consumer.seek(new TopicPartition("wangbaohua002",3),0);

        while (true){
            //拉取数据
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Integer.MAX_VALUE));


            //处理数据的业务逻辑
            for (ConsumerRecord<String, String> record : poll) {
                String topic = record.topic();
                int partition = record.partition();
                String value = record.value();
                long offset = record.offset();
                System.out.println(topic+","+partition+","+offset+","+value);
                Thread.sleep(30);
            }

            //手动提交偏移量  只可能重复读，不可能漏读
            //同步提交
            consumer.commitSync();



        }



    }
}
